//
//  VerfityCodeViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/18.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "VerfityCodeViewController.h"
#import "VerificationCodeBtn.h"
@interface VerfityCodeViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
@property(nonatomic,strong)UITextField *phoneTextFiled;
@property(nonatomic,strong)UITextField *secrectTextFiled;
@property(nonatomic,strong)VerificationCodeBtn *timeBtn;
@end

@implementation VerfityCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backForePage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(30));
        make.left.equalTo(self.view.mas_left).offset(10);
        make.width.mas_equalTo(scaleLeftAndRight(23));
        make.height.mas_equalTo(scaleTopAndBottom(23));
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backButton.mas_right).offset(scaleLeftAndRight(10));
        make.width.mas_equalTo(scaleLeftAndRight(2));
        make.height.mas_equalTo(scaleTopAndBottom(25));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(30));
    }];
    UILabel *findSecLabel = [[UILabel alloc] init];
    findSecLabel.text = self.backStr;
    [self.view addSubview:findSecLabel];
    [findSecLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view1.mas_right).offset(scaleLeftAndRight(10));
        make.width.mas_equalTo(scaleLeftAndRight(200));
        make.height.mas_equalTo(scaleTopAndBottom(25));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(30));
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"我们已发送验证码到您的手机";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view1.mas_bottom).offset(scaleTopAndBottom(30));
        make.left.equalTo(self.view.mas_left);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    UILabel *phoneNumberlabel = [[UILabel alloc] init];
    phoneNumberlabel.textAlignment = NSTextAlignmentCenter;
    phoneNumberlabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    [self.view addSubview:phoneNumberlabel];
    [phoneNumberlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleLeftAndRight(5));
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    //******************第一个输入框布局*********************
    UIImageView *phoneImage = [[UIImageView alloc] init];
   phoneImage.image = [UIImage imageNamed:@"密码"];
    [self.view addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
        make.top.equalTo(phoneNumberlabel.mas_bottom).offset(scaleTopAndBottom(85));
        make.width.mas_equalTo(scaleLeftAndRight(15));
        make.height.mas_equalTo(scaleTopAndBottom(15));
    }];
        self.timeBtn = [[VerificationCodeBtn alloc] initWithFrame:CGRectZero];
        [self.view addSubview:self.timeBtn];
        [self.timeBtn startTimer];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-40));
        make.top.equalTo(phoneNumberlabel.mas_bottom).offset(scaleTopAndBottom(85));
        make.width.mas_equalTo(scaleLeftAndRight(30));
        make.height.mas_equalTo(scaleTopAndBottom(15));
    }];
    self.phoneTextFiled = [[UITextField alloc] init];
    self.phoneTextFiled.placeholder = @"请输入验证码";
    self.phoneTextFiled.delegate = self;
    [self.view addSubview:self.phoneTextFiled];
    [self.phoneTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage.mas_right).offset(scaleLeftAndRight(5));
        make.top.equalTo(phoneNumberlabel.mas_bottom).offset(scaleTopAndBottom(78));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.right.equalTo(self.timeBtn.mas_left).offset(scaleLeftAndRight(-5));
    }];
    UIView *view2 = [[UIView alloc] init];
    [self.view addSubview:view2];
    view2.backgroundColor = [UIColor lightGrayColor];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-30));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(30));
        make.top.equalTo(self.phoneTextFiled.mas_bottom).offset(scaleTopAndBottom(0));
        make.height.mas_equalTo(scaleTopAndBottom(1));
    }];
    ////******************第二个输入框布局*********************
    UIImageView *setSecrtImage = [[UIImageView alloc] init];
    setSecrtImage.image = [UIImage imageNamed:@"密码"];
    [self.view addSubview:setSecrtImage];
    [setSecrtImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
        make.top.equalTo(view2.mas_bottom).offset(scaleTopAndBottom(45));
        make.width.mas_equalTo(scaleLeftAndRight(15));
        make.height.mas_equalTo(scaleTopAndBottom(15));
    }];
    UIButton *visualImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [visualImageBtn setImage:[UIImage imageNamed:@"眼睛"] forState:UIControlStateNormal];
    [visualImageBtn addTarget:self action:@selector(visualInputWord:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:visualImageBtn];
    [visualImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-40));
        make.top.equalTo(view2.mas_bottom).offset(scaleTopAndBottom(45));
        make.width.mas_equalTo(scaleLeftAndRight(15));
        make.height.mas_equalTo(scaleTopAndBottom(15));
    }];
    self.secrectTextFiled = [[UITextField alloc] init];
    self.secrectTextFiled.delegate = self;
    self.secrectTextFiled.placeholder = @"请设置4-16位新密码";
    self.secrectTextFiled.secureTextEntry = YES;
    [self.view addSubview:self.secrectTextFiled];
    [self.secrectTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(setSecrtImage.mas_right).offset(scaleLeftAndRight(5));
        make.top.equalTo(view2.mas_bottom).offset(scaleTopAndBottom(38));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.right.equalTo(visualImageBtn.mas_left).offset(scaleLeftAndRight(-5));
    }];
    UIView *view3 = [[UIView alloc] init];
    [self.view addSubview:view3];
    view3.backgroundColor = [UIColor lightGrayColor];
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-30));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(30));
        make.top.equalTo(self.secrectTextFiled.mas_bottom).offset(0);
        make.height.mas_equalTo(scaleTopAndBottom(1));
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.backgroundColor = ZColorRGB(244.0, 190.0, 65.0);
    loginBtn.layer.cornerRadius = scaleTopAndBottom(25);
    loginBtn.layer.masksToBounds = YES;
    [loginBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(registerNewAcountInformation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(scaleTopAndBottom(50));
        make.top.equalTo(view3.mas_bottom).offset(scaleTopAndBottom(70));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(40));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-40));
    }];
}
#pragma mark-创建倒计时的按钮布局
-(void)createTimeButton{
    
    VerificationCodeBtn *timeBtn = [[VerificationCodeBtn alloc] initWithFrame:CGRectZero];
    [self.view addSubview:timeBtn];
    [timeBtn setBackgroundColor:[UIColor lightGrayColor]];
    
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.bottom.equalTo(self.view.mas_bottom).offset(500);
        make.right.equalTo(self.view.mas_right).offset(-100);
        make.width.mas_equalTo(100);
    }];
    __block VerificationCodeBtn *blockBtn = timeBtn;
    [blockBtn startTimer];
    timeBtn.clickblock = ^{
        [blockBtn startTimer];
        //获取手机的验证码
    };
}
#pragma mark -获取验证码
-(void)getVerifyCode{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/customerApp/getSmsMail";
    NSString *phoneStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    NSDictionary *dict = @{@"phoneNumber":phoneStr,@"type":self.type};
    [manager POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark -返回
-(void)backForePage{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark -密码可见
-(void)visualInputWord:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setImage:[UIImage imageNamed:@"眼镜后"] forState:UIControlStateNormal];
        self.secrectTextFiled.secureTextEntry = NO;
    }else{
        [btn setImage:[UIImage imageNamed:@"眼睛"] forState:UIControlStateNormal];
        self.secrectTextFiled.secureTextEntry = YES;
    }
}
#pragma mark -用户注册新的账号
-(void)registerNewAcountInformation{
    if (self.secrectTextFiled.text.length < 6 || self.secrectTextFiled.text.length > 32) {
        UIAlertView *alerview = [[UIAlertView alloc] initWithTitle:@"密码只能设置6位-32位" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alerview show];
        
    }else{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString *url = ServerIp@"/customerApp/registerByPhoneNumber";
        NSString *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
        NSDictionary *dict = @{@"phoneNumber":phoneNumber,@"passWord":self.secrectTextFiled.text,@"phoneCode":self.phoneTextFiled.text};
        [manager POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dict);
            if ([dict[@"result"] integerValue] == 0) {
                //********************测试登录*******************************
                if ([self respondsToSelector:@selector(presentingViewController)]){
                    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
                }
                else {
                    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
                }
            }else{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:dict[@"msg"] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alertView.delegate = self;
                [alertView show];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
 
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
